<template>
  <el-dialog
    :title="!dataForm.welderid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="焊工名称" prop="weldername">
      <el-input v-model="dataForm.weldername" placeholder="焊工名称"></el-input>
    </el-form-item>
    <el-form-item label="班组" prop="weldergroup">
      <el-input v-model="dataForm.weldergroup" placeholder="班组"></el-input>
    </el-form-item>
    <el-form-item label="电流超规范次数" prop="curunnormal">
      <el-input v-model="dataForm.curunnormal" placeholder="电流超规范次数"></el-input>
    </el-form-item>
    <el-form-item label="电压超规范次数" prop="volunnormal">
      <el-input v-model="dataForm.volunnormal" placeholder="电压超规范次数"></el-input>
    </el-form-item>
    <el-form-item label="焊丝超规范次数" prop="pgmunnormal">
      <el-input v-model="dataForm.pgmunnormal" placeholder="焊丝超规范次数"></el-input>
    </el-form-item>
    <el-form-item label="气体超规范次数" prop="remunnormal">
      <el-input v-model="dataForm.remunnormal" placeholder="气体超规范次数"></el-input>
    </el-form-item>
    <el-form-item label="总超规范" prop="allunnormal">
      <el-input v-model="dataForm.allunnormal" placeholder="总超规范"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          welderid: 0,
          weldername: '',
          weldergroup: '',
          curunnormal: '',
          volunnormal: '',
          pgmunnormal: '',
          remunnormal: '',
          allunnormal: ''
        },
        dataRule: {
          weldername: [
            { required: true, message: '焊工名称不能为空', trigger: 'blur' }
          ],
          weldergroup: [
            { required: true, message: '班组不能为空', trigger: 'blur' }
          ],
          curunnormal: [
            { required: true, message: '电流超规范次数不能为空', trigger: 'blur' }
          ],
          volunnormal: [
            { required: true, message: '电压超规范次数不能为空', trigger: 'blur' }
          ],
          pgmunnormal: [
            { required: true, message: '焊丝超规范次数不能为空', trigger: 'blur' }
          ],
          remunnormal: [
            { required: true, message: '气体超规范次数不能为空', trigger: 'blur' }
          ],
          allunnormal: [
            { required: true, message: '总超规范不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.welderid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.welderid) {
            this.$http({
              url: this.$http.adornUrl(`/welderunnormal/welderunnormal/info/${this.dataForm.welderid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.weldername = data.welderunnormal.weldername
                this.dataForm.weldergroup = data.welderunnormal.weldergroup
                this.dataForm.curunnormal = data.welderunnormal.curunnormal
                this.dataForm.volunnormal = data.welderunnormal.volunnormal
                this.dataForm.pgmunnormal = data.welderunnormal.pgmunnormal
                this.dataForm.remunnormal = data.welderunnormal.remunnormal
                this.dataForm.allunnormal = data.welderunnormal.allunnormal
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/welderunnormal/welderunnormal/${!this.dataForm.welderid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'welderid': this.dataForm.welderid || undefined,
                'weldername': this.dataForm.weldername,
                'weldergroup': this.dataForm.weldergroup,
                'curunnormal': this.dataForm.curunnormal,
                'volunnormal': this.dataForm.volunnormal,
                'pgmunnormal': this.dataForm.pgmunnormal,
                'remunnormal': this.dataForm.remunnormal,
                'allunnormal': this.dataForm.allunnormal
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
